package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.domain.TenderExpertexpertGroup;
import com.ruoyi.system.domain.TenderSuppliers;
import com.ruoyi.system.service.ITenderExpertexpertGroupService;
import com.ruoyi.system.service.ITenderSuppliersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TenderItem;
import com.ruoyi.system.service.ITenderItemService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目管理Controller
 * 
 * @author ruoyi
 * @date 2025-05-12
 */
@Controller
@RequestMapping("/system/item")
public class TenderItemController extends BaseController
{
    private String prefix = "system/item";

    @Autowired
    private ITenderItemService tenderItemService;
    @Autowired
    private ITenderExpertexpertGroupService tenderExpertexpertGroupService;
    @Autowired
    private ITenderSuppliersService tenderSuppliersService;

    @RequiresPermissions("system:item:view")
    @GetMapping()
    public String item()
    {
        return prefix + "/item";
    }

    /**
     * 查询项目管理列表
     */
    @RequiresPermissions("system:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TenderItem tenderItem)
    {
        startPage();
        List<TenderItem> list = tenderItemService.selectTenderItemList(tenderItem);
        for (TenderItem item : list) {
            item.setGroupName(tenderExpertexpertGroupService.selectTenderExpertexpertGroupByGroupId(item.getGroupId()).getGroupName());
            item.setSupplierName(tenderSuppliersService.selectTenderSuppliersBySupplierId(item.getSupplierId()).getSupplierName());
        }
        return getDataTable(list);
    }

    /**
     * 查询项目管理列表--根据登录用户获取用户组id，在找到对应的项目列表
     */
    @RequiresPermissions("system:item:list")
    @PostMapping("/list1")
    @ResponseBody
    public TableDataInfo list1(TenderItem tenderItem)
    {
        startPage();
        List<TenderItem> list = tenderItemService.selectTenderItemList1(tenderItem);
        for (TenderItem item : list) {
            item.setGroupName(tenderExpertexpertGroupService.selectTenderExpertexpertGroupByGroupId(item.getGroupId()).getGroupName());
            item.setSupplierName(tenderSuppliersService.selectTenderSuppliersBySupplierId(item.getSupplierId()).getSupplierName());
        }
        return getDataTable(list);
    }

    /**
     * 导出项目管理列表
     */
    @RequiresPermissions("system:item:export")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TenderItem tenderItem)
    {
        List<TenderItem> list = tenderItemService.selectTenderItemList(tenderItem);
        ExcelUtil<TenderItem> util = new ExcelUtil<TenderItem>(TenderItem.class);
        return util.exportExcel(list, "项目管理数据");
    }

    /**
     * 新增项目管理
     */
    @RequiresPermissions("system:item:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<TenderExpertexpertGroup> tenderExpertexpertGroups = tenderExpertexpertGroupService.selectTenderExpertexpertGroupAll();
        List<TenderSuppliers> tenderSuppliers = tenderSuppliersService.selectTenderSuppliersAll();
        mmap.put("tenderExpertexpertGroups",tenderExpertexpertGroups );
        mmap.put("tenderSuppliers",tenderSuppliers );
        return prefix + "/add";
    }

    /**
     * 新增保存项目管理
     */
    @RequiresPermissions("system:item:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TenderItem tenderItem)
    {
        return toAjax(tenderItemService.insertTenderItem(tenderItem));
    }

    /**
     * 修改项目管理
     */
    @RequiresPermissions("system:item:edit")
    @GetMapping("/edit/{itemId}")
    public String edit(@PathVariable("itemId") Long itemId, ModelMap mmap)
    {
        TenderItem tenderItem = tenderItemService.selectTenderItemByItemId(itemId);
        mmap.put("tenderItem", tenderItem);
        List<TenderExpertexpertGroup> tenderExpertexpertGroups = tenderExpertexpertGroupService.selectTenderExpertexpertGroupAll();
        List<TenderSuppliers> tenderSuppliers = tenderSuppliersService.selectTenderSuppliersAll();
        mmap.put("tenderExpertexpertGroups",tenderExpertexpertGroups );
        mmap.put("tenderSuppliers",tenderSuppliers );
        return prefix + "/edit";
    }

    /**
     * 修改保存项目管理
     */
    @RequiresPermissions("system:item:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TenderItem tenderItem)
    {
        return toAjax(tenderItemService.updateTenderItem(tenderItem));
    }

    /**
     * 删除项目管理
     */
    @RequiresPermissions("system:item:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tenderItemService.deleteTenderItemByItemIds(ids));
    }
}
